# -*- coding:utf-8 -*-
"""

"""

import requests
import json
from WriteData import writedata
from Tk import genearteMD5
from cs import day


def post_html_text(url, data):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.post(url, timeout=30, data=data, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_dates_kx(url, data):
    text = post_html_text(url, data)
    if not text:
        return None
    text = json.loads(text)
    data = text['result']
    dates = [date['created'][:10] for date in data]
    id = ''
    for x in data:
        id = x['id']
    return (dates, id)


def count_process_one():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    id = 99999999
    url = 'http://www.liansiling.com/blga/mobile/findContentList'
    while a < 15:
        data = {
            "typeSlug": "kuaixun",
            "upOrDown": "down",
            "orderId": id,
            "typeSlugHtml": "notNull",
        }
        dates = get_dates_kx(url, data)
        if not dates:
            continue
        if not dates:
            continue
        print('----链司令获取第{}页----'.format(a))
        id = dates[1]
        for date in dates[0]:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '链司令'
    t1 = count_process_one()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0], "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
